package com.zrf.control;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zrf.pojo.User;
import com.zrf.service.ProductService;
import com.zrf.service.StorageService;
import com.zrf.service.UserService;

@RequestMapping("/uc")
@Controller
public class UserControl{
	@Autowired
	private UserService us;
	@Autowired
	private ProductService ps;
	@Autowired
	private StorageService ss;
	
	@RequestMapping(value="first", params={"name","sid"})
	public String first(@RequestParam("name") String name,
						@RequestParam("sid") Integer sid,
						Map<String, Object> mp){
		mp.put("uname", name);
		mp.put("storage", ps.getStorageById(sid));
		mp.put("allp", ps.getAllProduct(sid));
		return "first";
	}
	
	@RequestMapping(value="login", params={"name","pwd"})
	public String login(@RequestParam("name") String name,@RequestParam("pwd") String pwd){
		if (us.login(name, pwd)) {
			return "redirect:/uc/first?name="+name+"&sid="+1;
		}
		return "index";
	}
	
	@RequestMapping(value="yaddp", params={"uname"})
	public String yaddp(Map<String, Object> mp,
						@RequestParam("uname") String name){
		User user = us.getUserByName(name);
		mp.put("user", user);
		mp.put("sid", user.getStorage().getSid());
		mp.put("name", "商品添加");
		return "save/addOrUpdateP";
	}

}